package com.wjw.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.wjw.bean.User;

/**
 * 登陆拦截器
 * @author admin
 *
 */
public class LoginInterceptor implements HandlerInterceptor{

	/**
	 * 完成视图渲染后执行此方法
	 */
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 控制器执行完毕后执行的逻辑操作
	 */
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 在控制器执行前完成业务逻辑操作
	 * 返回值决定逻辑是否继续执行（即是否走到控制层)
	 */
	//springmvc-context里配置拦截器
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		HttpSession session=request.getSession();
		String path=session.getServletContext().getContextPath();
		
		User user=(User)session.getAttribute("user");
		if(user==null){
			response.sendRedirect(path+"/login");
			return false;
		}else{
			return true;
		}
	}

}
